﻿#include "stdafx.h"
#include "Logic.h"
#include "MobileGameDlg.h"
#include "resource.h"

//写任务进度
void WriteTaskSpeed(INT iDmIndex, CString strTask, INT roleIndex)
{
	CTime time = CTime::GetCurrentTime();
	CString strTime = time.Format(L"%Y-%m-%d");
	CString str;
	str.Format(L"%s_%s_%d", strTime, strTask, roleIndex);
	CString strAccount = m_ScriptSet[iDmIndex].m_GameInfoSt.m_strAccount;
	g_TaskSpeedFile.SetInt(strAccount, str, 1);
}

//读任务进度
BOOL ReadTaskSpeed(INT iDmIndex, CString strTask, INT roleIndex)
{
	CTime time = CTime::GetCurrentTime();
	CString strTime = time.Format(L"%Y-%m-%d");
	CString str;
	str.Format(L"%s_%s_%d", strTime, strTask, roleIndex);
	DbgOut(L"[sdn] 任务进度-%s", str);
	CString strAccount = m_ScriptSet[iDmIndex].m_GameInfoSt.m_strAccount;
	return g_TaskSpeedFile.GetInt(strAccount, str);
}

//获取日期和时间
void GetDateAndTime(int& iCurWeekDay, int& iCurHour)
{
	CTime t = CTime::GetCurrentTime(); //获取系统日期
									   //int d = t.GetDay(); //获得几号
									   //int y = t.GetYear(); //获取年份
									   //int m = t.GetMonth(); //获取当前月份
	iCurHour = t.GetHour(); //获取当前为几时
							//int mm = t.GetMinute(); //获取分钟
							//int s = t.GetSecond(); //获取秒
	iCurWeekDay = t.GetDayOfWeek(); //获取星期几，注意1为星期天，7为星期六
}


void MoveClick(INT iDmIndex, LONG x, LONG y, BOOL bFlag)
{
	int iRet = RandomNumber(2, 6);
	m_DM[iDmIndex]->MoveTo(x + iRet, y + iRet);
	Sleep(200);
	if (bFlag)
		m_DM[iDmIndex]->LeftDoubleClick();
	else
		m_DM[iDmIndex]->LeftClick();
	Sleep(300);
}
void KeyPressNum(long vx,int num) {
	for (int i = 0; i < num; i++){
		Sleep(200);
		m_GlobalDM->LockInput(1);
		m_GlobalDM->KeyPress(vx);
		m_GlobalDM->LockInput(0);
	}
}

void KeyPressEX(INT iDmIndex, CString ch)
{
	m_DM[iDmIndex]->LockInput(1);
	m_DM[iDmIndex]->KeyPressChar(ch);
	m_DM[iDmIndex]->LockInput(0);
}

//拖动不释放
void Drag_NoUp(int iDmIndex, INT x1, INT y1, INT x2, INT y2, INT iTime)
{

	m_DM[iDmIndex]->MoveTo(x1, y1);
	Sleep(50);
	m_DM[iDmIndex]->LeftDown();
	Sleep(50);
	m_DM[iDmIndex]->MoveTo(x2, y2);
	Sleep(iTime);
}

void Drag(int iDmIndex, INT x1, INT y1, INT x2, INT y2, INT iTime)
{
	m_DM[iDmIndex]->MoveTo(x1, y1);
	Sleep(50);
	m_DM[iDmIndex]->LeftDown();
	Sleep(50);
	m_DM[iDmIndex]->MoveTo(x2, y2);
	Sleep(iTime);
	m_DM[iDmIndex]->LeftUp();
}

//移动拖拽
void ClickAndDrag(int iDmIndex, INT x1, INT y1, INT x2, INT y2)
{

	m_DM[iDmIndex]->LockInput(1);
	m_DM[iDmIndex]->MoveTo(x1, y1);
	Sleep(100);
	m_DM[iDmIndex]->LeftDown();
	Sleep(100);

	if (x1 < x2)
	{
		while (true)
		{
			x1 = x1 + 1;
			m_DM[iDmIndex]->MoveTo(x1, y1);
			Sleep(10);
			if (x1 == x2)
				break;
			else
			{
			}
		}
	}
	if (x1 > x2)
	{
		while (true)
		{
			x1 = x1 - 1;
			m_DM[iDmIndex]->MoveTo(x1, y1);
			Sleep(10);
			if (x1 == x2)
				break;
			else
			{
			}
		}
	}
	if (y1 < y2)
	{
		while (true)
		{
			y1 = y1 + 1;
			m_DM[iDmIndex]->MoveTo(x1, y1);
			Sleep(10);
			if (y1 == y2)
				break;
			else
			{
			}
		}
	}
	if (y1 > y2)
	{
		while (true)
		{
			y1 = y1 - 1;
			m_DM[iDmIndex]->MoveTo(x1, y1);
			Sleep(10);
			if (y1 == y2)
				break;
		}
	}

	m_DM[iDmIndex]->LeftUp();
	Sleep(100);
	m_DM[iDmIndex]->LockInput(0);
}

//显示控制台每个账号的各种信息
void ShowConsoleInfo(int iDmIndex, int iSubIndex)
{
	switch (iSubIndex)
	{
	case 1://句柄
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, NumberToString(m_ScriptSet[iDmIndex].m_hGameHandle));

	}break;
	case 4:
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strServer);
	}break;
	case 5://序号
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, NumberToString(m_ScriptSet[iDmIndex].m_GameInfoSt.m_iRoleIndex));

	}break;
	case 6://银币
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strMoney);
	}break;
	case 7://等级
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strLv);

	}break;
	case 8://模拟器序号
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, NumberToString(iDmIndex));

	}break;
	case 9://任务状态
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState);

	}break;
	case 10://运行状态
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strRunState);

	}
	default:
		break;
	}
}


//获取父窗口的子窗口句柄
LONG GetSubWindow(LONG father)
{
	CString strClass, strTitle;
	LONG pWnd, pWnd1 = NULL;
	TCHAR buffer[256];
	TCHAR buffer1[256];
	CRect rect;
	pWnd = (LONG) ::GetWindow((HWND)father, GW_CHILD);     // 查找对话框中的子控件

	while (pWnd)
	{
		::GetClassName((HWND)pWnd, buffer, 256);
		strClass = buffer;
		::GetWindowText((HWND)pWnd, buffer1, 256);
		strTitle = buffer1;
		::GetWindowRect((HWND)pWnd, &rect);

		if (strClass == "RenderWindow"&& strTitle == "TheRender" && rect.Width() == 800 && rect.Height() == 600)//"RenderWindowWindow")
			return (LONG)pWnd;

		pWnd1 = GetSubWindow(pWnd);       // 查找下一个子控件
		if (pWnd1)
			return pWnd1;
		else
			pWnd = (LONG) ::GetNextWindow((HWND)pWnd, 2);
	}
	return pWnd;
}

void sendAdsl() {
	bool flag = true;
	while (flag)
	{
		CString sAdsl, sAdslInfo, sAdslName, endAdsl;

		sAdslName = arrAdsl[g_IniFile.GetInt(L"Set", L"AdslIndex")];//vpn下标
		endAdsl.Format(L"rasdial %s /disconnect", g_IniFile.GetString(L"Set", L"AdslServer"));
		GetCmdResult(endAdsl, sAdslInfo);
		Sleep(1000);
		sAdslInfo = "";
		for (int i = 0; i < 10; i++)
		{
			
			GetCmdResult(endAdsl, sAdslInfo);
			sAdslInfo = "";
			Sleep(1000);
			sAdsl.Format(L"rasdial %s %s 123", g_IniFile.GetString(L"Set",L"AdslServer"), sAdslName);
			TCHAR* cAdsl = CString2TCHAR(sAdsl);
			GetCmdResult(cAdsl, sAdslInfo);
			int nIndex = sAdslInfo.Find(L"error");
			if (nIndex > 0) {
				g_pPage1->SetDlgItemTextW(IDC_STATIC_PAGE1_STATE, sAdsl.Mid(nIndex + 5, 4));//连接错误
				Sleep(2000);
				CString sDelay;
				sDelay += L".";
				g_pPage1->SetDlgItemTextW(IDC_STATIC_PAGE1_STATE, L"连接错误等待重新拨号" + sDelay);//连接错误
				Sleep(1000);
				
			}
			adsl.GetAdslStyle();
			CString sIP = adsl.GetAdslIp();
			if (sAdslInfo.Find(L"Command completed successfully") > 0 || sIP != L"") {
				g_IniFile.SetString(L"Set", L"IP", L"");
				Sleep(3000);
				int j = 0, k = 0;
				for (int i = 0; i < 20; i++)
				{
					if (!CheckIPAddr()) {//不存在
						j++;
					}
					else {
						k++;
					}
					Sleep(500);
				}
				if (j > k)//连接成功
				{
					g_pPage1->SetDlgItemTextW(IDC_STATIC_PAGE1_IP, GetNetIp());
					g_pPage1->SetDlgItemTextW(IDC_STATIC_PAGE1_CODE, L"当前vpn账号:" + sAdslName);
					g_IniFile.SetString(L"Set", L"IP", GetNetIp());
				}
			}
			//adsl.GetAdslStyle();
			//CString sIP = adsl.GetAdslIp();
			//if (sAdslInfo.Find(L"Command completed successfully")>0|| sIP !=L"") {
			//	int nAdslNum = g_IniFile.GetInt(L"Set", L"AdslNum");
			//	g_IniFile.SetString(L"Set", L"IP",L"");
			//	Sleep(1000);
			//	CString str = GetNetIp();
			//	
			//	g_pPage1->SetDlgItemTextW(IDC_STATIC_PAGE1_IP, GetNetIp());
			//	g_pPage1->SetDlgItemTextW(IDC_STATIC_PAGE1_CODE, L"当前vpn账号:"+ sAdslName);
			//	
			//	//设置配置
			//	g_IniFile.SetString(L"Set", L"IP", adsl.GetAdslIp());
			//	
			//	//if (nAdslNum == 4) {
			//	//	//vpn下标+1
			//	//	g_IniFile.SetInt(L"Set", L"AdslIndex", g_IniFile.GetInt(L"Set", L"AdslIndex") + 1);
			//	//	g_IniFile.SetInt(L"Set", L"AdslNum", 0);
			//	//}
			//	return;
			//}
			//如果拨号失败 vpn连接下一个
		}

		g_IniFile.SetInt(L"Set", L"AdslIndex", g_IniFile.GetInt(L"Set", L"AdslIndex") + 1);
		g_IniFile.SetInt(L"Set", L"AdslNum", 0);
		g_IniFile.SetString(L"Set", L"IP", L"");
		g_pPage1->SetDlgItemTextW(IDC_STATIC_PAGE1_STATE, L"连接下一个IP");
		Sleep(1000);
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////
//处理当前任务的流程
int DealCurTaskProcess(int nIndex)
{
	while (true)//检测是否已经连接
	{
		if (g_IniFile.GetString(L"Set", L"IP") != "") {
			break;
		}
		DbgOut("等待连接1");
		Sleep(3000);
	}
	if (!StartLoginClient(nIndex)) {
		return 0;
	}
	if (!StartLoginDebug(nIndex)) {
		return 0;
	}
	MainTask(nIndex);
	return 0;
}


bool StartLoginClient(int nIndex) {
	HRESULT hr = CoInitialize(NULL);
	if (SUCCEEDED(hr))
	{

		int nCount = 0;
		VARIANT _x, _y;
		while (m_GState)
		{
		Part:
			m_GlobalDM->UnBindWindow();
			long nMapHwnd = (long)m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
			if (nMapHwnd > 0) {
				OutPutListView(nIndex, L"关闭客户端");
				KillProcess(L"MapleStory.exe");
			}
			nMapHwnd = (long)m_GlobalDM->FindWindow(L"", L"MapleStory");
			if (nMapHwnd > 0) {
				OutPutListView(nIndex, L"关闭登录器");
				KillProcess(L"MapleStory.exe");
				Sleep(200);
				KillProcess(L"GameLauncher.exe");
			}
			OutPutListView(nIndex, L"查找登录");
			m_GlobalDM->delay(1000);
			CString sPath;
			g_pPage1->GetDlgItemTextW(IDC_EDIT_Simulator_Path, sPath);
			m_GlobalDM->FindPic(0, 0, 1366, 768, L"game.bmp|1280960game.bmp", L"202020", 0.9, 0, &_x, &_y);
			m_GlobalDM->MoveTo(_x.lVal + 15, _y.lVal + 5);
			m_GlobalDM->delay(500);
			m_GlobalDM->LeftDoubleClick();
			m_GlobalDM->delay(500);

			nMapHwnd = (long)m_GlobalDM->FindWindow(L"", L"MapleStory");
			if (nMapHwnd <= 0) {

				goto Part;
			}
			m_GlobalDM->SetWindowState((long)nMapHwnd, 12);
			m_GlobalDM->delay(1000);
			//m_GlobalDM->MoveWindow((long)nMapHwnd, -10, 0);
			m_GlobalDM->delay(1000);
			int ret = m_GlobalDM->BindWindowEx((long)nMapHwnd, L"normal", L"windows3", L"windows", L"", 0);
			FindPic(0, 0, 1366, 768, L"loginBtn.bmp|1366768game.bmp|1280960game2.bmp", &_x, &_y);
			if (_x.lVal > 0) {
				m_GlobalDM->delay(1000);
				if (_x.lVal >= 349) {
					m_GlobalDM->MoveTo(_x.lVal, _y.lVal + 5);
				}
				else {
					m_GlobalDM->MoveTo(_x.lVal + 259, _y.lVal + 38);
				}

				m_GlobalDM->delay(1000);
				m_GlobalDM->LeftClick();
			}
			m_GlobalDM->UnBindWindow();
			m_GlobalDM->delay(500);
			for (int i = 0; i < 120; i++)
			{
				//m_GlobalDM->IsBind()
				//查找登录成功标志
				m_GlobalDM->delay(2000);
				//OutPutListView(nIndex, L"查找客户端");
				m_GGameHwnd = (long)m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
				if (m_GGameHwnd > 0 && m_GlobalDM->IsBind(m_GGameHwnd) == 0) {
					OutPutListView(nIndex, L"绑定客户端");
					ret = m_GlobalDM->BindWindowEx(m_GGameHwnd, L"gdi", L"normal", L"normal", L"", 0);
					OutPutListView(nIndex, L"绑定成功");
					if (ret != 1) {
						g_IniFile.SetInt(L"Set", L"Num", nIndex);
						m_GState = false;
						return false;
					}
				}
				m_GlobalDM->FindPic(0, 0, 800, 600, L"gameLogo.bmp|code.bmp|password.bmp", L"202020", 0.9, 0, &_x, &_y);
				if (_x.lVal > 0) {
					m_CheckState = true;
					OutPutListView(nIndex, L"游戏登录成功");
					return true;
				}

			}
			g_IniFile.SetInt(L"Set", L"Num", nIndex);
			m_GState = false;
		}
		return true;
	}
}

//循环登录
bool StartLoginDebug(int nIndex)
{
	HRESULT hr = CoInitialize(NULL);
	if (SUCCEEDED(hr))
	{

		VARIANT _x, _y;
		int nCount = 0;
		long nMapHwnd = (long)m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
		//int ret = m_GlobalDM->BindWindowEx((long)nMapHwnd, L"gdi", L"normal", L"windows", L"", 0);

		bool flag = false;
		//m_GlobalDM->MoveWindow(nMapHwnd, 0, 0);
		//m_GlobalDM

		while (m_GState)
		{
			//m_GlobalDM->SetWindowState(m_GGameHwnd, 12);
			if (runScript(L"reset")) {
				g_IniFile.SetInt(L"Set", L"Num", nIndex);
				m_GState = false;
				return false;
			}
			if (runScript(L"loginQiehuan")) {
				//代理账号问题切换Ip
				g_IniFile.SetInt(L"Set", L"StartAdsl", 0);
				g_IniFile.SetInt(L"Set", L"AdslIndex", g_IniFile.GetInt(L"Set", L"AdslIndex") + 1);
				g_IniFile.SetString(L"Set", L"IP", L"");
				Sleep(5000);
				//关闭程序
				KillProcess(L"MapleStory.exe");
				KillProcess(L"GameLauncher.exe");
				//关闭客户端
				m_CheckState = false;
				m_GState = false;
				return false;
			}
			
			if (runScript(L"codeError") || runScript(L"codeError1")) {
				g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"2");
				OutPutListView(nIndex, L"账号密码错误");
				g_MobileGameDlg->m_list_info.SetItemText(nIndex, 6, L"账号密码错误");
				++m_GCount;
				return false;
			}
			if (runScript(L"twoPwdError")) {
				g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"3");
				OutPutListView(nIndex, L"二密错误");
				g_MobileGameDlg->m_list_info.SetItemText(nIndex, 6, L"二密错误");
				g_IniFile.SetInt(L"Set", L"Num", nIndex + 1);

				return false;
			}
			m_GlobalDM->MoveToEx(100, 100, 30, 45);
			m_GlobalDM->FindPic(294, 278, 437, 311, L"code.bmp", L"202020", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				OutPutListView(nIndex, L"输入账号");
				m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 10, 5);
				m_GlobalDM->delay(1000);
				m_GlobalDM->LeftClick();
				m_GlobalDM->delay(1000);
				m_GlobalDM->SendString2(nMapHwnd, g_MobileGameDlg->m_list_info.GetItemText(nIndex, 1));
				m_GlobalDM->delay(1000);
			}
			m_GlobalDM->delay(2000);
			m_GlobalDM->MoveToEx(100, 100, 30, 45);
			m_GlobalDM->delay(500);
			m_GlobalDM->FindPic(283, 308, 447, 332, L"password.bmp", L"202020", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				OutPutListView(nIndex, L"输入密码");
				m_GlobalDM->MoveTo(_x.lVal + 50, _y.lVal + 8);
				Sleep(1000);
				m_GlobalDM->LeftClick();
				Sleep(1000);
				m_GlobalDM->SendString2(nMapHwnd, g_MobileGameDlg->m_list_info.GetItemText(nIndex, 2));
				Sleep(2000);
			}
			m_GlobalDM->FindPic(453 - 5, 298 - 5, 487 + 5, 317 + 5, L"login.bmp", L"202020", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				OutPutListView(nIndex, L"点击登录");
				Sleep(1000);
				m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 10, 5);
				m_GlobalDM->LeftClick();
			}
			m_GlobalDM->FindPic(330 - 5, 282 - 5, 359 + 5, 302 + 5, L"checkCode.bmp", L"202020", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				OutPutListView(nIndex, L"选择账号");
				m_GlobalDM->MoveTo(_x.lVal - RandomNumber(10, 30), _y.lVal + 46);
				m_GlobalDM->LeftDoubleClick();
				m_GlobalDM->delay(2000);
			}

			Sleep(2000);
			m_GlobalDM->FindPic(98 - 5, 0, 147 + 5, 23 + 5, L"loginEnter.bmp", L"202020", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				OutPutListView(nIndex, L"回车选择");
				m_GlobalDM->KeyPress(13);
				m_GlobalDM->delay(2000);
			}
			m_GlobalDM->FindPic(271 - 5, 190 - 5, 336 + 5, 214 + 5, L"loginEnter1.bmp", L"202020", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				OutPutListView(nIndex, L"回车选择");
				m_GlobalDM->KeyPress(13);
				m_GlobalDM->delay(2000);
			}
			m_GlobalDM->FindPic(625 - 5, 401 - 5, 719 + 5, 430 + 5, L"twoCodeWnd.bmp", L"202020", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				OutPutListView(nIndex, L"回车选择");
				m_GlobalDM->KeyPress(13);

				m_GlobalDM->delay(2000);
			}
			m_GlobalDM->FindPic(133 - 10, 203 - 10, 241 + 10, 219 + 10, L"twoCodeKey.bmp", L"303030", m_fSim, 0, &_x, &_y);
			//if (runScript(L"twoCodeKey")) {

			//}

			CString str;
			str.Format(L"查找二密窗口%d", _x.lVal);
			OutPutListView(nIndex, str);
			if (runScript(L"twoCodeKey") > 0 && !flag) {
				m_GlobalDM->MoveToEx(100, 100, 30, 45);
				CString code = g_MobileGameDlg->m_list_info.GetItemText(nIndex, 3);
				CString code1;
				for (int i = 0; i < code.GetLength(); i++)
				{

					m_GlobalDM->delay(RandomNumber(800,1500));
					code1 = code1 + code.Mid(i, 1);
					CString strbmp = code.Mid(i, 1) + L".bmp";
					CString sTwoCode = L"输入二密" + code.Mid(i, 1);

					OutPutListView(nIndex, sTwoCode);
					if (i > 0) {
						CString code3 = code.Mid(i, 1);
						CString code2 = code.Mid(i - 1, 1);

						if (code3 == code2) { //前后数据相同
							m_GlobalDM->MoveToEx(RandomNumber(300, 100), 100, 30, 45);
							m_GlobalDM->delay(200);
						}
					}

					m_GlobalDM->FindPic(126, 222, 675, 417, strbmp, L"202020", 0.90, 0, &_x, &_y);
					//
					if (_x.lVal > 0) {
						m_GlobalDM->MoveToEx(_x.lVal + 10, _y.lVal + 5, 10, 10);
						m_GlobalDM->LeftClick();
						m_GlobalDM->delay(1500);



					}
					m_GlobalDM->MoveToEx(RandomNumber(100, 200), 100, 30, 45);

				}
				for (int i = 0; i < 5; i++)
				{
					m_GlobalDM->FindPic(298, 415, 399, 441, L"enterCode.bmp", L"303030", m_fSim, 0, &_x, &_y);
					if (_x.lVal > 0) {
						OutPutListView(nIndex, L"二密确认");
						m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 10, 5);
						m_GlobalDM->LeftClick();
						m_GlobalDM->delay(1500);
						flag = true;
					}
				}
			}
			//
			m_GlobalDM->FindPic(281, 213, 524, 350, L"twoCodeError.bmp", L"303030", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				//TODO: 二密错误 处理 
			}



			if (flag) {
				m_GlobalDM->delay(1500);
				if (runScript(L"taskRun") || runScript(L"loginSucess")) {
					return true;
				}
				m_GlobalDM->FindPic(685, 538, 799, 588, L"loginSuccess.bmp|loginSuccess1.bmp", L"303030", m_fSim, 0, &_x, &_y);
				{
					if (_x.lVal > 0) {
						return true;
					}
				}
			}
			nCount++;
			if (nCount > 30) {//启动客户端失败
				g_IniFile.SetInt(L"Set", L"Num", nIndex);
				m_GState = false;
				return false;
			}
			m_GlobalDM->delay(1500);

		}
	}
}

void ZhiXian(int iDmIndex)
{
}
//开始任务
int MainTask(int nIndex)
{

	HRESULT hr = CoInitialize(NULL);
	if (SUCCEEDED(hr))
	{

		//m_GGameHwnd = (long)m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
		//int ret = m_GlobalDM->BindWindowEx(m_GGameHwnd, L"gdi", L"normal", L"normal", L"", 0);

		int j = 0;
		CString str;
		CloseHandle(::CreateThread(NULL, 0, OnlinCheck, NULL, 0, NULL));


		while (m_GState)
		{
			m_GlobalDM->MoveToEx(100, 100, 100, 100);
			m_GlobalDM->SetWindowState(m_GGameHwnd, 12);
			OutPutListView(nIndex, L"执行任务");
			CString taskStr = L"loginOkWnd|closeWnd|closeWnd1|taskRun|taskDong|taskBossTag|taskBossOk";
			runScriptList(taskStr);

			if (runScript(L"taskGameOver")) {
				OutPutListView(nIndex, L"任务完成");
				int nAdslNum = g_IniFile.GetInt(L"Set", L"AdslNum");
				g_IniFile.SetInt(L"Set", L"AdslNum", nAdslNum + 1);
				g_pPage1->SetDlgItemTextW(IDC_STATIC_PAGE1_ADSLNUM, NumberToString(g_IniFile.GetInt(L"Set", L"AdslNum")));
				if (g_IniFile.GetInt(L"Set", L"AdslNum") == 1) {
					//vpn下标+1
					g_IniFile.SetInt(L"Set", L"AdslIndex", g_IniFile.GetInt(L"Set", L"AdslIndex") + 1);
					g_IniFile.SetInt(L"Set", L"AdslNum", 0);
					g_IniFile.SetString(L"Set", L"IP", L"");
				}
				g_IniFile.SetInt(L"Set", L"Num", nIndex + 1);
				g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"1");
				g_MobileGameDlg->m_list_info.SetItemText(nIndex, 6, L"完成");
				KillProcess(L"MapleStory.exe");
				KillProcess(L"GameLauncher.exe");
				g_IniFile.SetInt(L"Set", L"StartAdsl", 0);
				//关闭客户端

				m_CheckState = false;
				m_GState = false;
				return true;
			}
			//查找npc
			if (runScript(L"taskNpc")) {
				OutPutListView(nIndex, L"找到Npc");
				Sleep(2000);
				for (int i = 0; i < 5; i++)
				{
					runScript(L"taskChoseOne");
					runScript(L"taskChoseNext");
					//if (runScript(L"taskGameOver")) {
					//	g_AccountFile.SetString(NumberToString(nIndex + 1), L"state", L"1");
					//	return true;
					//}

					// 任务结束
					//设置任务状态为已完成
					//++m_GCount;

					Sleep(500);
				}

				if (runScript(L"taskReady")) {

					OutPutListView(nIndex, L"等待组队");
					int k = 0;
					while (true)
					{
						Sleep(1000);
						OutPutListView(nIndex, L"等待组队.");
						Sleep(200);
						OutPutListView(nIndex, L"等待组队..");
						Sleep(200);
						OutPutListView(nIndex, L"等待组队....");
						if (runScript(L"taskTeamWaitOk")) {
							m_GlobalDM->MoveTo(100, RandomNumber(200, 300));
							m_GlobalDM->MoveTo(100, RandomNumber(200, 300));
							m_GlobalDM->MoveTo(100, RandomNumber(200, 300));
							OutPutListView(nIndex, L"组队成功");
						}

						if (runScript(L"taskBossYes")) {
							OutPutListView(nIndex, L"进入boss");
							if (k == 0) {
								for (int i = 0; i < 3; i++)
								{
									int nrightNum = m_GlobalDM->GetColorNum(850, 444, 1024, 470, L"fbe9a2", 0.8);
									int nleftNum = m_GlobalDM->GetColorNum(0, 444, 70, 470, L"fbe9a2", 0.8);
									if (nrightNum < 10) {
										for (int i = 0; i < 3; i++)
										{
											str.Format(L"右边移动%d", i);
											OutPutListView(nIndex, str);
											moveKey(39, 200, 200);
											m_GlobalDM->KeyPress(16);
											moveKey(16, 200, 200);

										}
									}
									if (nleftNum < 10) {
										for (int i = 0; i < 3; i++)
										{
											str.Format(L"左边移动%d", i);
											OutPutListView(nIndex, str);
											moveKey(37, 500, 200);
											m_GlobalDM->KeyPress(16);
											moveKey(16, 200, 200);
										}
									}
									m_GlobalDM->KeyPress(16);
									moveKey(16, 200, 200);
								}
								Sleep(1000);

							}
							bossAttack(nIndex);
							k++;
						}


						if (runScript(L"bossEnd")) {
							OutPutListView(nIndex, L"boss任务结束");
							m_GlobalDM->KeyPress(13);
							for (size_t i = 0; i < 100; i++)
							{
								//查找退出路口
								moveKey(39, 400, 200);
								Sleep(1000);
								OutPutListView(nIndex, L"查找出口");
								if (runScript(L"taskBossEndOut")) {
									if (intX.lVal >= 985 && intX.lVal < 999) {
										OutPutListView(nIndex, L"点击出口");
										m_GlobalDM->KeyPress(38);
										moveKey(38, 500, 200);

									}
									else {
										moveKey(37, 500, 200);

										m_GlobalDM->KeyPress(38);
										moveKey(38, 500, 200);


									}
								}
								if (runScript(L"taskNpcTag")) {
									break;
								}
								if (i > 50) {
									if (!runScript(L"taskBossYes")) {
										break;
									}
								}

							}
							break;
						}
					}
				}
			}
			else {
				//m_GlobalDM->SetWindowState(m_GGameHwnd, 12);
				OutPutListView(nIndex, L"移动查找任务Npc");
				m_GlobalDM->delay(500);
				m_GlobalDM->KeyDown(39);
				m_GlobalDM->delay(2000);
				m_GlobalDM->delay(500);
				m_GlobalDM->KeyUp(39);
			}
		}
		return 0;
	}
}



//////////////////////////////////////////////////////////////////////////////////////////
//换角色
int ChangeRole(int iDmIndex)
{
	
	return 0;
}


void WanShiTongDeKaoYan(int iDmIndex) {

}
void XueYuanRenWu(int iDmIndex) {

}

void OutPutListView(int nIndex,CString str) {
	g_MobileGameDlg->m_list_info.SetItemText(nIndex, 5, str);

}


void FindPic(long x1, long y1, long x2, long y2, CString str, VARIANT * x, VARIANT * y,long offsetX,long offsetY) {
	m_GlobalDM->FindPic(x1-5, y1-5, x2+5, y2+5, str, L"303030", m_fSim, 0, x, y);
}

void FindPicEx(long x1, long y1, long x2, long y2, CString str, long offsetX, long offsetY) {
	VARIANT _x, _y;
	FindPic(x1, y1, x2, y2, str, &_x, &_y, offsetX, offsetY);
	if (_x.lVal > 0) {
		m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, offsetX, offsetY);
		m_GlobalDM->LeftClick();
		m_GlobalDM->delay(1000);
	}
}

void bossAttack(int nIndex) {
	void addBlood();
	int nrightNum = m_GlobalDM->GetColorNum(900, 444, 1024, 470, L"fbe9a2", 0.9);
	int nleftNum = m_GlobalDM->GetColorNum(0, 444, 170, 470, L"fbe9a2", 0.9);
	//for (int i = 0; i < 10; i++)
	//{
	CString str;
	OutPutListView(nIndex, L"查找开始");
	if (nrightNum <= 10 && nleftNum <= 10) { //boss占满屏幕直接攻击
		OutPutListView(nIndex, L"boss占满屏幕");
		for (int i = 0; i < 10; i++)
		{
			addBlood();
			moveKey(16, 200, 200);
		}
	}
	if (nleftNum < 10) {
		for (int i = 0; i < 5; i++) //行走左边7次每次查找boss是否存在
		{
			addBlood();
			nleftNum = m_GlobalDM->GetColorNum(0, 444, 170, 470, L"fbe9a2", 0.8);
			if (nleftNum <= 5) {//找到boss攻击
				OutPutListView(nIndex, L"找到左边位置");
				for (int i = 0; i < 10; i++)
				{
					if (i < 3) {
						str.Format(L"左边移动%d", i);
						OutPutListView(nIndex, str);
						moveKey(37, 200, 200);
						m_GlobalDM->KeyPress(16);
						moveKey(16, 200, 200);
					}
					if (i > 5) {
						 nrightNum = m_GlobalDM->GetColorNum(850, 444, 1024, 470, L"fbe9a2", 0.8);
						 if (nrightNum < 0) {
							 break;
						}
					}
					str.Format(L"左边攻击%d", i);
					m_GlobalDM->KeyPress(16);
					OutPutListView(nIndex, str);
					addBlood();
					moveKey(16, 200, 200);
				}
			}
			else {//未找到移动

				OutPutListView(nIndex, L"左边移动");
				moveKey(37, 500, 200);
			}
			nrightNum = m_GlobalDM->GetColorNum(850, 444, 1024, 470, L"fbe9a2", 0.8);
			if (nrightNum <= 0) {
				break;
			}
		}
	}
	if (nrightNum < 10) {
		for (int i = 0; i < 8; i++) //行走右7次每次查找boss是否存在
		{
			addBlood();
			nrightNum = m_GlobalDM->GetColorNum(850, 444, 1024, 467, L"fbe9a2", 0.8);
			if (nrightNum <= 5) {//找到boss攻击
				OutPutListView(nIndex, L"找到右边位置");
				for (int i = 0; i < 10; i++)
				{
					if (i < 3) {
						str.Format(L"右边移动%d", i);
						OutPutListView(nIndex, str);
						moveKey(39, 200, 200);
						m_GlobalDM->KeyPress(16);
						moveKey(16, 200, 200);
					}
					if (i > 5) {
						nleftNum = m_GlobalDM->GetColorNum(0, 444, 170, 470, L"fbe9a2", 0.8);
						if (nleftNum < 0) {
							break;
						}
					}
					addBlood();
					moveKey(16, 200, 200);
					m_GlobalDM->KeyPress(16);
					str.Format(L"右边攻击%d", i);
					OutPutListView(nIndex, str);
				}
			}
			else {//未找到移动
				OutPutListView(nIndex, L"右边移动");
				moveKey(39, 300, 200);
			}
			nleftNum = m_GlobalDM->GetColorNum(0, 444, 170, 470, L"fbe9a2", 0.8);
			if (nleftNum <= 0) {
				break;
			}
		}
	}
	//TODO: 跳查找
	OutPutListView(nIndex, L"查找结束");
}
//补血
void addBlood() {
	if (m_GlobalDM->CmpColor(569, 717, L"fb6bd1", 0.9) == 1) {
		m_GlobalDM->KeyPress(35);
	}
	
}

void moveKey(int key, int delayDown, int delayUp) {
		m_GlobalDM->KeyDown(key);
		m_GlobalDM->delay(delayDown);
		m_GlobalDM->KeyUp(key);
		m_GlobalDM->delay(delayUp);
}
//检测是否掉线
void checkOnLine() {

}
